package org.clipjoint.service.impl;

import org.clipjoint.dao.UserDao;
import org.clipjoint.dao.impl.UserDaoImpl;
import org.clipjoint.entity.Purchased;
import org.clipjoint.entity.User;
import org.clipjoint.entity.Dream;
import org.clipjoint.service.UserService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    //邮箱注册
    @Override
    public int register(User user) throws SQLException {
        // 创建时间为当前时间
        user.setCreate_time(new Date());
        user.setAccount(200L);
        user.setCredit(100L);
        return userDao.insert(user);
    }

    //用户登录
    @Override
    public User login(User user) throws SQLException {
        User result = userDao.selectByEmailAndPassword(user);

        // 登录成功记录最后登录时间
        if (result != null) {
            result.setLast_login_time(new Date());
            userDao.update(result);
        }

        return result;
    }

    @Override
    public List<Dream> dream(Long id) throws SQLException {
        return userDao.dream(id);
    }

    @Override
    public List<Purchased> purchased(Long id) throws SQLException {
        return userDao.purchased(id);
    }


    @Override
    public int updateInfo(User user) throws SQLException {
        int updateResult = userDao.update(user);
        return updateResult;
    }

    //确认邮箱
    @Override
    public boolean checkEmail(String email) throws SQLException {

        User result = userDao.selectByEmail(User.builder().email(email).build());

        return result == null;
    }
}
